package com.youdianda.service.impl;

import com.youdianda.dto.SysconfigDTO;
import com.youdianda.entity.SysConfig;
import com.youdianda.mapper.SysConfigMapper;
import com.youdianda.result.Result;
import com.youdianda.service.ISysConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.vo.SysConfigBaseVO;
import com.youdianda.vo.SysConfigList;
import com.youdianda.vo.SysConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sdy
 * @since 2023-05-15
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 所有配置查询
     *
     * @return
     */
    @Override
    public Result all() {
        //第一步
        //查询该表所有数据
        List<SysConfig> sysConfigs = sysConfigMapper.selectList();
        //创建总的返回对象 将所有数据遍历到allData里面
        SysConfigVO sysConfigVO = new SysConfigVO();
        List<SysConfig> allData = new ArrayList<>();
        Map<String, String> configForm = new HashMap<>();
        Map<String, String> configRules = new HashMap<>();
        List<SysConfigList> list = new ArrayList<>();
        //获取list 创建三个list对象
        SysConfigList baseVO = new SysConfigList();
        SysConfigList systemVO = new SysConfigList();
        SysConfigList customVO = new SysConfigList();
        //创建三个list对象里面的children集合
        List baseList = new ArrayList<>();
        List systemList = new ArrayList<>();
        List customList = new ArrayList<>(); //children

        //创建基础配置的三个子对象 站点-1 底部-2 其他-3
        SysConfigBaseVO sysConfigBaseVO1 = new SysConfigBaseVO();
        SysConfigBaseVO sysConfigBaseVO2 = new SysConfigBaseVO();
        SysConfigBaseVO sysConfigBaseVO3 = new SysConfigBaseVO();

        //创建基础配置三个子对象对应的集合 用来存储数据 站点-1 底部-2 其他-3
        List<SysConfig> baseVListO1 = new ArrayList<>();
        List<SysConfig> baseVListO2 = new ArrayList<>();
        List<SysConfig> baseVListO3 = new ArrayList<>();

        //创建系统设置的对象 管理系统配置
        SysConfigBaseVO systemBaseVO1 = new SysConfigBaseVO();

        //创建系统设置对象的对应的子集合 用来存储数据
        List<SysConfig> systemListO1 = new ArrayList<>();

        //创建其他设置对象  通知公告-1 前台图片-2
        SysConfigBaseVO systemcustomVO1 = new SysConfigBaseVO(); //SysConfigBaseVO
        SysConfigBaseVO systemcustomVO2 = new SysConfigBaseVO(); //SysConfigBaseVO

        //创建其他设置对象的对应的子集合 用来存储数据 通知公告-1 前台图片-2
        List<SysConfig> customListO1 = new ArrayList<>();
        List<SysConfig> customListO2 = new ArrayList<>();


        //创建三个集合 children 作为list对象的
        sysConfigs.forEach(sysConfig -> {
            //存储allData数据
            allData.add(sysConfig);
            String attrKey = sysConfig.getAttrKey();
            String attrValue = sysConfig.getAttrValue();
            configForm.put(attrKey, attrValue);

            String validator1 = sysConfig.getValidator();
            if (validator1 != null && validator1!= "") {
                String attrKey1 = sysConfig.getAttrKey();
                String validator = sysConfig.getValidator();
                configRules.put(attrKey1, validator);
            }
            //对tab_value进行判断
            String tabValue = sysConfig.getTabValue();
            if (tabValue.equals("base")) {
                String tabValue1 = sysConfig.getTabValue();
                baseVO.setKey(tabValue1);
                baseVO.setName("基础配置");
//                baseList.add(sysConfig);
                String groupValue = sysConfig.getGroupValue();
                if (groupValue.equals("站点配置")) {
                    sysConfigBaseVO1.setName(groupValue);
                    baseVListO1.add(sysConfig);
                } else if (groupValue.equals("底部信息")) {
                    sysConfigBaseVO2.setName(groupValue);
                    baseVListO2.add(sysConfig);
                } else {
                    sysConfigBaseVO3.setName("其他配置");
                    baseVListO3.add(sysConfig);
                }


            } else if (tabValue.equals("system")) {
                String tabValue1 = sysConfig.getTabValue();
                systemVO.setKey(tabValue1);
                systemVO.setName("系统设置");
//                systemList.add(sysConfig);
                systemBaseVO1.setName("管理系统配置");
                systemListO1.add(sysConfig);

            } else {
                String tabValue1 = sysConfig.getTabValue();
                customVO.setKey(tabValue1);
                customVO.setName("其他设置");
//                customList.add(sysConfig);
                String groupValue = sysConfig.getGroupValue();
                if (groupValue.equals("通知公告")) {
                    systemcustomVO1.setName("通知公告");
                    customListO1.add(sysConfig);
                } else {
                    systemcustomVO2.setName("前台图片");
                    customListO2.add(sysConfig);
                }

            }


        });

        sysConfigBaseVO1.setData(baseVListO1);
        sysConfigBaseVO2.setData(baseVListO2);
        sysConfigBaseVO3.setData(baseVListO3);



        systemBaseVO1.setData(systemListO1);


        systemcustomVO1.setData(customListO1);
        systemcustomVO2.setData(customListO2);


        baseList.add(sysConfigBaseVO1);
        baseList.add(sysConfigBaseVO2);
        baseList.add(sysConfigBaseVO3);


        systemList.add(systemBaseVO1);


//
        customList.add(systemcustomVO1);
        customList.add(systemcustomVO2);


        baseVO.setChildren(baseList);
        systemVO.setChildren(systemList);
        customVO.setChildren(customList);


        list.add(baseVO);
        list.add(systemVO);
        list.add(customVO);


        sysConfigVO.setAllData(allData);//封装allData
        sysConfigVO.setConfigForm(configForm);//封装configForm
        sysConfigVO.setConfigRules(configRules);//封装configRules  1
        sysConfigVO.setList(list);//封装list


        return Result.success(sysConfigVO);
    }

    /**
     * 修改系统配置
     *
     * @param dtoList
     * @return
     */
    @Override
    public Result updatenew(List<SysconfigDTO> dtoList) {
        for (SysconfigDTO sysconfigDTO : dtoList) {
            SysConfig sysConfig = new SysConfig();
            BeanUtils.copyProperties(sysconfigDTO,sysConfig);
            String attr_key = sysconfigDTO.getAttr_key();
            String attr_value = sysconfigDTO.getAttr_value();
            sysConfig.setAttrKey(attr_key);
            sysConfig.setAttrValue(attr_value);
            updateById(sysConfig);
        }

        return Result.success();
    }


}
